package com.example.wan1.base.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.wan1.R;
import com.scwang.smart.refresh.footer.BallPulseFooter;
import com.scwang.smart.refresh.header.MaterialHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/***
 * @Author: zxd
 * @CreateDate: 2021/5/13 10:54
 * @Description: 创建一个基类列表碎片
 * 过段时间研究吧，暂时没有办法能做到，因为P曾注入的问题
 */
public abstract class ListFragment extends Fragment {

    protected Context mContext;
    protected Unbinder mUnbinder;

    @BindView(R.id.srlheader)
    MaterialHeader mSrlheader;
    @BindView(R.id.rv_list)
    RecyclerView mRvList;
    @BindView(R.id.srlfooter)
    BallPulseFooter mSrlfooter;
    @BindView(R.id.srl)
    SmartRefreshLayout mSrl;

    RecyclerView.Adapter mAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_list, container, false);
        mContext = getActivity();
        mUnbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mAdapter = getAdapter();
        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(mContext));
        mSrl.setEnableLoadMore(true);
        mSrl.setEnableRefresh(true);
    }

    protected abstract RecyclerView.Adapter getAdapter();

    @Override
    public void onDestroy() {
        if (mUnbinder != null)
            mUnbinder.unbind();
        super.onDestroy();
    }
}
